var q = require('q');
var request = require('request').defaults({
    json: true
});


var config = require('./../config.json');


exports.getMonitorPoints = function () {
    var _this = this;
    var defer = q.defer();
    var url = config.apiUrl + "/MonitorPointsGallery";
    request({
        url: url,
        qs: {
            prjId: _this.prjId
        }
    }, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            if (typeof (body) === 'string') {
                body = JSON.parse(body);
            }
            _this.monitorPoints = body;
            defer.resolve(body);
        } else {
            defer.resolve([]);
        }
    });
    return defer.promise;
};

exports.getRecentChanges = function (params) {
    var defer = q.defer();
    var url = config.apiUrl + "/recentchanges";
    request({
        url: url,
        qs: params
    }, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            if (typeof (body) === 'string') {
                body = JSON.parse(body);
            }
            defer.resolve(body);
        } else {
            defer.resolve({});
        }
    });
    return defer.promise;
};

exports.getSinglePointChange = function (params) {
    var defer = q.defer();
    var url = config.apiUrl + "/singlePointRecentChanges";
    request({
        url: url,
        qs: params
    }, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            if (typeof (body) === 'string') {
                body = JSON.parse(body);
            }
            defer.resolve(body);
        } else {
            defer.resolve({});
        }
    });
    return defer.promise;
};

exports.stasticReportSummary = function (startTime, endTime) {
    var _this = this;
    _this.getMonitorPoints()
        .then(function (monitorPoints) {
            monitorPoints.forEach( function (monitorPointsGroup) {
                monitorPointsGroup.locationPoints.forEach( function (locationPoint) {
                    var type = monitorPointsGroup.monitorType;
                    var location = locationPoint.location;
                });
            })
        });
};